<?php
/**
 * Created by WuYe.
 * User: '常志明'
 * Date: 2018-10-31 14:23
 * 培训管理
 */

namespace app\index\controller;


class TrainingManage
{
    //考试试卷
    const trainingpaper = [
        'storeId'=>'',      //门店id
        'paperName'=>'',    //试卷名字
    ];
    //试卷题目
    const trainingquestions = [
        'paperId'=>'',     //试卷id
        'topic'=>'',       //题目
        'options'=>[],     //选项
        'result'=>'',      //结果
    ];

    public static function getData($where, $one = true, $table = 'trainingpaper')
    {
        return ($one == true) ? getFirst(mongo($table)->where($where)->select()) : mongo($table)->where($where)->select();
    }
    public static function setData($where, $data, $table = 'trainingpaper')
    {
        mongo($table)->where($where)->update($data);
    }
    public static function insertData($data, $table = 'trainingpaper')
    {
        mongo($table)->insert($data);
        return mongo($table)->getLastInsID();
    }
    public static function deleteData($where, $table = 'trainingpaper')
    {
        mongo($table)->where($where)->delete();
    }

    /**
     *  新建试卷
     * @param string $paperName  试卷名称
     * @return array
     */
    public static function newPaper($paperName = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $paperName = input('paperName');
        $storeId   = User::getStoreId();
        $paperId   = self::insertData(['storeId'=>$storeId, 'paperName'=>$paperName]);
        return RD(SUCCESS, $paperId);
    }
    /**
     * 获取所有试卷
     * @return array
     */
    public static function getPapers()
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $pageSize = input('pageSize');
        $currentPage = input('currentPage');

        $storeId = User::getStoreId();
        $result =  self::getData(['storeId'=>$storeId], false);
        foreach ($result as $key=>$value) {
            $result[$key]['quantity'] =  count(self::getData(['paperId'=>$value['id']],false));
        }
        $count = count($result);
        array_paging($result, ($currentPage - 1) * $pageSize, $pageSize);
        return RD(SUCCESS, ['total'=>$count, 'data'=>$result]);
    }
    /**
     * 重命名试卷名称
     * @param string $id         试卷id
     * @param string $paperName  试卷名称
     */
    public static function renamePaper($id = '', $paperName = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $id        = input('id');
        $paperName = input('paperName');

        self::setData(['id'=>$id], ['paperName'=>$paperName]);
        return RD(SUCCESS);
    }

    /**
     * 删除试卷
     * @param string $id  试卷id
     */
    public static function deletePaper($id = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $id        = input('id');
        self::deleteData(['id'=>$id]);      //删除考题
        self::deleteData(['paperId'=>$id]); //删除试卷
        return RD(SUCCESS);
    }

    /**
     * 添加考题
     * @param string $id      试卷id
        @param string $topic   题目
        @param array  $options 选项[]
        @param string $result  结果
     */
    public static function addExaminationQuestions($id = '', $topic = '', $options = [], $result = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $id     = input('id');
        $topic  = input('topic');
        $options= input('options/a', []);
        $result = input('result');

        $insertData = [
            'paperId'  => $id,
            'topic'    => $topic,
            'options'  => $options,
            'result'   => $result,
        ];
        self::insertData($insertData);
        return RD(SUCCESS);
    }

    /**
     *  获取所有考题
     * @param string $id   试卷id
     */
    public static function getExaminationQuestions($id = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $pageSize = input('pageSize');
        $currentPage = input('currentPage');
        $id = input('id');
        $result = self::getData(['paperId'=>$id], false);

        $count = count($result);
        array_paging($result, ($currentPage - 1) * $pageSize, $pageSize);
        return RD(SUCCESS, ['total'=>$count, 'data'=>$result]);
    }
    /**
     * 编辑考题
     * @param string $id      考题id
        @param string $topic   题目
        @param array  $options 选项[]
        @param string $result  结果
     */
    public static function editExaminationQuestions($id = '', $topic = '', $options = [], $result = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $id     = input('id');
        $topic  = input('topic');
        $options= input('options/a', []);
        $result = input('result');

        $updateData = [
            'topic'    => $topic,
            'options'  => $options,
            'result'   => $result,
        ];
        self::setData(['id'=>$id], $updateData);
        return RD(SUCCESS);
    }
    /**
     * 删除考题
     * @param string $id 考题id
     */
    public static function deleteExaminationQuestions($id = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $id     = input('id');
        self::deleteData(['id'=>$id]);
        return RD(SUCCESS);
    }
}